﻿Shader "TShaderTest/MagicVertexColor"
{
    Properties
    {
        _Color ("Tint", Color) = (1,1,1,1)
    }

    SubShader
    {
        Tags
        { 
            "Queue"="Transparent" 
            "RenderType"="Transparent" 
        }

        Cull Back
        Lighting Off
        ZWrite On
        Blend SrcAlpha OneMinusSrcAlpha
        
        Pass
        {
        CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "Lighting.cginc"
            struct appdata_t
            {
                float4 vertex   : POSITION;
                float4 color    : COLOR;
                float2 texcoord : TEXCOORD0;
                float3 normal   : NORMAL;
            };

            struct v2f
            {
                float4 vertex   : SV_POSITION;
                fixed4 color    : COLOR;
                half2 texcoord  : TEXCOORD0;
            };

            fixed4 _Color;

            v2f vert(appdata_t a2v)
            {
                v2f OUT;
                OUT.vertex = UnityObjectToClipPos(a2v.vertex);
                OUT.texcoord = a2v.texcoord;
                
                fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                fixed3 worldNormal = normalize(mul(a2v.normal,(float3x3)unity_WorldToObject));
                //取光照方向，因为是平行光，所以，出射方向就是光源的位置
                fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
                
                //计算漫反射颜色
                fixed3 diffuse = _LightColor0.rgb * max(dot(worldNormal,worldLight),0.0);
                
//                //高光反射计算
//                //反射光方向计算：平行光的入射方向，法线方向
//                fixed3 reflectDir = normalize(reflect(-worldLight,worldNormal));
//                //视野方向计算：摄像机坐标 - 顶点坐标
//                fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - mul(a2v.vertex,unity_WorldToObject).xyz);
//                fixed3 specular = _LightColor0.rgb * pow(max(dot(reflectDir,viewDir),0),100)*0;
                
                OUT.color = a2v.color * fixed4(diffuse + ambient,1.0);

                return OUT;
            }


            fixed4 frag(v2f fOut) : SV_Target
            {
                return fOut.color;
            }
        ENDCG
        }
    }
}
